package com.fer.hr.nmbp.lab2.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.apache.ibatis.session.SqlSession;

import com.hr.fer.nmbp.lab2.domain.Room;
import com.hr.fer.nmbp.lab2.persistence.RoomMapper;
import com.hr.fer.nmbp.lab2.service.RoomDAO;

public class DeleteRoomWindow extends JFrame {
	private static final long serialVersionUID = -567211123074911088L;
	
	private JPanel contentPane;
	private JList jlRoomList;
	private DefaultListModel model;
	private List<Room> rooms;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DeleteRoomWindow frame = new DeleteRoomWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void fillListModel(){
		model.clear();
		SqlSession session = MainWindow.sessionFactory.openSession();
		RoomMapper mapper = session.getMapper(RoomMapper.class);
		rooms = mapper.selectByExample(null);
		
		for(int i=0;i<rooms.size();i++){
			model.add(i, rooms.get(i).toString());
		}
		
		session.commit();
		session.close();
	}
	
	private void deleteClick(){
		try{
			Room selectedRoom = rooms.get(jlRoomList.getSelectedIndex());
			SqlSession session = MainWindow.sessionFactory.openSession();
			RoomDAO roomDao = new RoomDAO(session);
			roomDao.deleteRoom(selectedRoom.getRoomNum());
			session.commit();
			session.close();
			fillListModel();
			JOptionPane.showMessageDialog(this, "Sucess");
		}catch(Exception e){
			JOptionPane.showMessageDialog(this, "Nite odabrali sobu", "Upozorenje", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void closeClick(){
		this.dispose();
	}
	/**
	 * Create the frame.
	 */
	public DeleteRoomWindow() {
		setTitle("Brisanje sobe");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 224, 289);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		model = new DefaultListModel();
		jlRoomList = new JList(model);
		jlRoomList.setBounds(10, 36, 188, 120);
		contentPane.add(jlRoomList);
		
		fillListModel();
		
		JLabel lblPopisSoba = new JLabel("Popis soba:");
		lblPopisSoba.setBounds(10, 11, 66, 14);
		contentPane.add(lblPopisSoba);
		
		JButton btDelete = new JButton("Pobrisi sobu");
		btDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteClick();
			}
		});
		btDelete.setBounds(10, 167, 188, 23);
		contentPane.add(btDelete);
		
		JButton btClose = new JButton("Izlaz");
		btClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeClick();
			}
		});
		btClose.setBounds(10, 212, 188, 23);
		contentPane.add(btClose);
	}

}
